//
//  HBMarketRadarGuide.m
//  Adjust
//
//  Created by tianlong on 2024/12/9.
//

#import "HBMarketRadarGuide.h"

@interface HBMarketRadarGuide ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *coverBtn;

@end

@implementation HBMarketRadarGuide

- (instancetype)initWithFrame:(CGRect)frame params:(NSDictionary *)params {
    self = [super initWithFrame:frame];
    if (self) {
        self.bgView = [[UIView alloc] initWithFrame:self.bounds];
        self.bgView.backgroundColor = UIColor.blackColor;
        self.bgView.alpha = 0.7;
        [self addSubview:self.bgView];
        
        CGFloat top = 200;
        CGFloat left = 16;
        CGFloat popWidth = 296;
        CGFloat popHeight = 263;
        
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:self.bgView.bounds];
        UIBezierPath *subPath2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(left, top, popWidth, popHeight) cornerRadius:8];
        [bezierPath appendPath:subPath2];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = bezierPath.CGPath;
        maskLayer.fillRule = kCAFillRuleEvenOdd;
        self.bgView.layer.mask = maskLayer;
        
        [self addArrowViewToSuperview:self.bgView atFrame:CGRectMake(300, 150, 100, 100)
                            baseWidth:130 baseHeight:126];
        
        self.coverBtn = [[UIButton alloc] initWithFrame:self.bounds];
        [self.coverBtn addTarget:self action:@selector(clickedButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.coverBtn];
        
        
        
    }
    return self;
}

- (void)clickedButton:(UIButton *)button {
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    for (UIView *sub in window.subviews) {
        if ([sub isKindOfClass:[HBMarketRadarGuide class]]) {
            [sub removeFromSuperview];
        }
    }
}

+ (void)show:(NSDictionary *)params {
    
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    
    BOOL find = NO;
    for (UIView *sub in window.subviews) {
        if ([sub isKindOfClass:[HBMarketRadarGuide class]]) {
            find = YES;
            break;
        }
    }
    if (!find) {
        HBMarketRadarGuide *bgView = [[HBMarketRadarGuide alloc] initWithFrame:window.bounds params:params];
        [window addSubview:bgView];
    }
}

+ (void)dismiss {
    
}

- (void)addArrowViewToSuperview:(UIView *)superview
                        atFrame:(CGRect)frame
                      baseWidth:(CGFloat)baseWidth
                     baseHeight:(CGFloat)baseHeight {
    UIView *arrowView = [[UIView alloc] initWithFrame:frame];
    [superview addSubview:arrowView];
    
    // 创建一个图形上下文
    UIGraphicsBeginImageContext(arrowView.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置填充颜色为白色且不透明
    [[UIColor whiteColor] setFill];
    
    // 计算箭头的顶点位置
    CGFloat tipY = baseHeight / 2.0;
    CGFloat tipX = frame.size.width / 2.0;
    
    // 绘制箭头底边
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, tipX - baseWidth / 2.0, tipY + baseHeight / 2.0);
    CGContextAddLineToPoint(context, tipX, tipY);
    CGContextAddLineToPoint(context, tipX + baseWidth / 2.0, tipY + baseHeight / 2.0);
    CGContextClosePath(context);
    CGContextFillPath(context);
    
    // 获取绘制后的图像并设置为视图的背景
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    arrowView.backgroundColor = [UIColor clearColor];
    arrowView.layer.contents = (id)image.CGImage;
}


@end
